package cn.kgc.tangcco.action;

import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.pojo.Clazz;
import cn.kgc.tangcco.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/10/30 15:09
 */
@RestController
@RequestMapping(value = "/clazz")
public class ClazzAction {
    @Autowired
    private ClazzService clazzService;

    @GetMapping(value = "/list")
    public ResponseText<List<Clazz>> queryClazzList() {
        return clazzService.queryClazzs(new Clazz());
    }

    @GetMapping(value = "/findOne")
    public ResponseText<Clazz> queryClazz(Integer id) {
        return clazzService.queryClazz(id);
    }

    @GetMapping(value = "/getOne")
    public ResponseText<Clazz> getClazz(Integer id) {
        return clazzService.getClazz(id);
    }

    @GetMapping(value = "/getOneJson")
    public ResponseText<Map<String, Object>> getOneJson(Integer id) {
        Map<String, Object> map = new HashMap<>();
        ResponseText<Clazz> responseText = clazzService.getClazz(id);
        Clazz clazz = responseText.getData();
        map.put("clazz", clazz);
        map.put("students", clazz.getStudents());
        return new ResponseText<>(responseText.getCode(), responseText.getMsg(), map);
    }

    @PutMapping(value = "/save")
    public ResponseText save(Clazz clazz, BindingResult bindingResult) {
        return clazzService.saveOrUpdate(clazz);
    }

    @DeleteMapping(value = "/remove/{id}")
    public ResponseText<Clazz> removeClazz(@PathVariable(name = "id") Integer id) {
        return clazzService.remove(id);
    }

    @GetMapping(value = "/count")
    public ResponseText<Long> count() {
        return clazzService.count();
    }

    @GetMapping(value = "/exists")
    public ResponseText<Boolean> exists(Integer id) {
        return clazzService.exists(id);
    }


}
